<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />
        
        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('system.files.quick Search Fields') })"
        >
            <!-- 可以在此处以插槽的方式设置一些自定义按钮 -->
            <el-button v-blur type="warning" style="margin-left: 12px;" @click="routePush('/admin/system/files/category')">文件分类</el-button>
        </TableHeader>
        <div style="background: #fff; padding: 13px 15px; padding-top: 5px;" class="search-box">
            <div>
                <BaInput
                    type="remoteSelect"
                    v-model="state.category_id"
                    :attr="{
                        multiple: true,
                        pk: 'id',
                        field: 'name',
                        'remote-url': 'admin/system.files.category/index',
                        placeholder: '请选择文件分类',
                        params: { isTree: true, treeAll: true }
                    }"
                />
            </div>
            <div>
                <span style="">合同：</span>
                <BaInput type="string" v-model="state.name" :attr="{ clearable: true, placeholder: '请输入合同名称'}" style="width: 180px;" />
            </div>

            <div>
                <span style="">描述：</span>
                <BaInput type="string" v-model="state.description" :attr="{ clearable: true, placeholder: '请输入描述'}" style="width: 180px;" />
            </div>

            <div>
                <span style="">添加时间：</span>
                <el-date-picker
                    v-model="state.create_time"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    :teleported="false"
                    format="YYYY-MM-DD HH:mm:ss"
                    value-format="YYYY-MM-DD HH:mm:ss" 
                    :default-time="defaultTime"
                />
            </div>

            <div>
                <el-button plain v-blur @click="searchIndex" type="primary">搜索</el-button>
                <el-button plain v-blur @click="resetIndex" type="info">重置</el-button>
            </div>
            
            <div style="clear: both; float: unset;"></div>
        </div>
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />
        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive  } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

defineOptions({
    name: 'system/files',
})



import { routePush } from '/@/utils/router'


import BaInput from '/@/components/baInput/index.vue'


const defaultTime = reactive([new Date(0,0,0,0,0,0), new Date(0,0,0,23,59,59)])

let state = reactive({
    category_id: null,
    name: null,
    description: null,
    create_time: null
})

const { t } = useI18n()
const tableRef = ref()
let optButtons = defaultOptButtons([])

// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'download', // 按钮名称
        title: 'download', // 鼠标放置时的 title 提示
        text: '下载文件', // 直接在按钮内显示的文字，title 有值时可为空
        type: 'success', // 按钮类型，请参考 element plus 的按钮类型
        icon: '', // 按钮 icon
        disabledTip: true, // tipButton 禁用 tip
        click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
            window.open(row.file)
        },
        display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return true
        },
        disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
            return row.file ? false : true;
        },
        attr: {} // 自定义el-button属性
    },
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: '编辑',
        type: 'primary',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: '删除',
        type: 'danger',
        icon: '',
        popconfirm: {
            confirmButtonText: t('delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        }
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

const baTable = new baTableClass(
    new baTableApi('/system/system.Files/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { 
                label: t('system.files.category_id'), 
                prop: 'category.name', 
                align: 'left', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                // render: 'url', 
                // customTemplate: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                //     return '<span>' + value + '</span>';
                // },
                // render: 'customRender', 
                // customRender: markRaw(CategoryName),
                render: 'string', 
                class: 'hover-blue',
                click: (row: TableRow, field: TableColumn) => {
                    baTable.table.filter!.search = [{ field:'category_id', val:row.category_id, operator:'=', render:'tag' }]
                    baTable.getIndex();
                },
                width: 200
             
            },
            { 
                label: t('system.files.name'), 
                prop: 'name', 
                align: 'left', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false, 
                render: 'string',
                class: 'hover-blue', 
                click: (row: TableRow, field: TableColumn) => {
                    console.log(row.file)
                    if(row.file != null){
                        window.open(row.file)
                    }
                },
                width: 200
                
            },
            
            { label: t('system.files.description'), prop: 'description', align: 'left', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('system.files.create_time'), prop: 'create_time', align: 'center', render: 'datetime', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('operate'), align: 'center', width: 250, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { category_id: null, name: null, description: null, file: null },
    }
)

provide('baTable', baTable)

const resetIndex = () => {
    baTable.table.filter!.search = []
    state.category_id = null
    state.name = null
    state.description = null
    state.create_time = null
    baTable.getIndex();
}

const searchIndex = () => {
    baTable.table.filter!.search = [];
    if(state.category_id) {
        baTable.table.filter!.search?.push({ field: 'category_id', val: state.category_id, operator:'IN' })
    }
    if(state.name) {
        baTable.table.filter!.search?.push({ field: 'name', val: state.name, operator:'LIKE' })
    }
    if(state.description) {
        baTable.table.filter!.search?.push({ field: 'description', val: state.description, operator:'LIKE' })
    }
    if(state.create_time) {
        let create_time = state.create_time[0] + ',' + state.create_time[1];
        baTable.table.filter!.search?.push({ field: 'create_time', val: create_time, operator:'RANGE', render: 'datetime' })
    }
    
    baTable.getIndex();
}



onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
    
})
</script>



<style lang="scss">
.hover-blue{
    cursor: pointer;
}
.hover-blue:hover{
    color: #409eff;
}
</style>

<style scoped lang="scss">
.file-name{
    color: red !important;;
}
.search-box > div{
    float: left;
    margin-right: 12px;
}
</style>
